/**
 * Created by ChenYong on 2017-08-09.
 *
 * 权限列表。
 */

$(function () {
    /** 姓名的 jQuery 对象 */
    var $loginName = $("#login_name");
    /** 账号的 jQuery 对象 */
    var $loginid = $("#loginid");
    /** 权限分配状态的 jQuery 对象 */
    var $permissionStatus = $("#permission_status");
    /** 搜索按钮的 jQuery 对象 */
    var $searchBtn = $("#search_btn");
    /** 权限列表的表格，DataTables 的 API 对象 */
    var permissionTable = null;

    // 监听权限分配状态下拉列表搜索
    $permissionStatus.change(function () {
        searchData();
    });

    // 点击搜索按钮
    $searchBtn.click(function (e) {
        searchData();
    });

    // 展示权限表格
    permissionTable = $("#permission_table").myDataTable({
        url: "web/permission/list.json",
        searchBtn: $searchBtn,
        sScrollY: "450px",
        sScrollX: true,
        sScrollXInner: "100%",
        aaSorting: [[6, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "checkbox",
            sTitle: "<label class='pos-rel' title='全选'><input type='checkbox' class='ace'><span class='lbl'></span></label>",
            sWidth: "50px",
            sClass: "center",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html("<label class='pos-rel'><input type='checkbox' class='ace' data-id='{0}'><span class='lbl'></span></label>".format(rowData.id));
            }
        }, {
            mData: "loginName",
            sName: "loginName",
            sTitle: "姓名",
            sWidth: "120px"
        }, {
            mData: "loginid",
            sName: "loginid",
            sTitle: "账号",
            sWidth: "120px"
        }, {
            mData: "roleNames",
            sName: "roleNames",
            sTitle: "拥有的角色",
            sWidth: "200px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);
                var html = "";
                var htmlTpl = "<span class='label label-lg label-primary m-5'>{0}</span>";

                if ($.isNotBlank(rowData.roleNames)) {
                    var data = rowData.roleNames.split("|");

                    $.each(data, function (i, e) {
                        html += htmlTpl.format(e);
                    });
                }

                $cell.html(html);
            }
        }, {
            mData: "resourceNames",
            sName: "resourceNames",
            sTitle: "拥有的资源",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);
                var html = "";
                var htmlTpl = "<span class='label label-lg label-primary m-5'>{0}</span>";

                if ($.isNotBlank(rowData.resourceNames)) {
                    var data = rowData.resourceNames.split("|");

                    $.each(data, function (i, e) {
                        html += htmlTpl.format(e);
                    });
                }

                $cell.html(html);
            }
        }, {
            mData: "permissionCodes",
            sName: "permissionCodes",
            sTitle: "拥有的权限",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);
                var html = "";
                var htmlTpl = "<span class='label label-lg label-primary m-5'>{0}</span>";

                if ($.isNotBlank(rowData.permissionCodes)) {
                    var data = rowData.permissionCodes.split("|");

                    $.each(data, function (i, e) {
                        html += htmlTpl.format(e);
                    });
                }

                $cell.html(html);
            }
        }, {
            mData: "permissionStatus",
            sName: "permissionStatus",
            sTitle: "权限分配状态",
            sWidth: "120px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                // 未分配
                if (rowData.permissionStatus == 0) {
                    $cell.html("<span class='label label-lg label-danger'>未分配</span>");

                    // 已分配
                } else if (rowData.permissionStatus == 1) {
                    $cell.html("<span class='label label-lg label-success'>已分配</span>");
                }
            }
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "100px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.find("div.action-buttons").append("<a class='c-orange' title='给【{0}】分配权限' data-toggle='tooltip' data-placement='left' href='{1}web/permission/update.html?id={2}'><i class='ace-icon iconfont icon-pencil bigger-200'></i></a>".format(rowData.loginName, globalBasePath, rowData.id));
            }
        }],
        successFn: function () {
            $("[data-toggle='tooltip']").tooltip();
        }
    });

    /**
     *
     * 搜索数据。
     *
     */
    function searchData() {
        // 姓名
        if ($.isNotBlank($loginName.val()) && ($loginName.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "姓名的长度不能大于50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 账号
        if ($.isNotBlank($loginid.val()) && ($loginid.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "账号的长度不能大于50个字符",
                type: "warn",
                close: 3000
            });

            return;
        }

        $loginName.val($.trim($loginName.val()));
        $loginid.val($.trim($loginid.val()));

        // 搜索开始…
        if ($.isNotNull(permissionTable)) {
            permissionTable.myAjaxData = {
                loginid: $loginid.val(),
                loginName: $loginName.val(),
                permissionStatus: $permissionStatus.val()
            };

            // 刷新权限表格（分页重置）
            permissionTable.ajax.reload();
        }
    }
});